import java.awt.event.*;
import javax.swing.*;

public class OCLineButton extends JButton{
	//	Local Variables
	private JFrame _parentFrame;
	private StoreLogic _logic;
	private QueuePanel _queueP;

	public OCLineButton(JFrame aParentFrame, StoreLogic alogic, QueuePanel aQueueP)
	{
		super("Open/Close selected queue");
		_parentFrame = aParentFrame;
		_logic = alogic;
		_queueP = aQueueP;
		this.addActionListener(new OpenCloseListener());
	}
	
	private class OpenCloseListener implements ActionListener 
	{
		public void actionPerformed(ActionEvent e) 
		{	
			//	Checks if the selected line is open and if the number of open lines are 1
			if (_logic.getLineStatus((_queueP.getSelectedQueue())) && _logic.getOpenLines() <= 1)
			{
				JOptionPane.showMessageDialog(_parentFrame, "Cant close anymore queues, at least one needs to be open."
						,"Error", 1);
			}	//	Closes a line Checks if the selected line is open and if the number of open lines bigger then 1
			if (_logic.getLineStatus((_queueP.getSelectedQueue())) && _logic.getOpenLines() > 1)
			{
				_logic.setLineStatus(_queueP.getSelectedQueue(), false);
				_logic.spreadOut(_queueP.getSelectedQueue());
			} //	Opens a line, Checks if the selected line is closed
			else if (!_logic.getLineStatus(_queueP.getSelectedQueue()))	
			{
				_logic.setLineStatus(_queueP.getSelectedQueue(), true);
			}
			_queueP.updateQPanel();
		}
	}
}
